package com.ruoyi.project.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.common.StaticMethod;
import com.ruoyi.project.domain.FinPackage;
import com.ruoyi.project.service.IFinPackageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import javax.annotation.Resource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.FinSubjectMonth;
import com.ruoyi.project.service.IFinSubjectMonthService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 科目余额Controller
 * 
 * @author abel
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/project/FinSubjectMonth")
public class FinSubjectMonthController extends BaseController
{
    @Resource
    private IFinSubjectMonthService finSubjectMonthService;
    @Resource
    private IFinPackageService finPackageService;

    /**
     * 查询科目余额列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubjectMonth:list')")
    @GetMapping("/list")
    public TableDataInfo list(FinSubjectMonth finSubjectMonth)
    {
        startPage();
        finSubjectMonth.setPackageId(ServletUtils.getPackageId());
        FinPackage finPackage = finPackageService.getFinPackageById(ServletUtils.getPackageId());
        if(StringUtils.isBlank(finSubjectMonth.getYearAndMonth())){
            finSubjectMonth.setYearAndMonth(StaticMethod.genYearAndMonth(finPackage.getCurrentYear(),finPackage.getCurrentMonth()));
        }
        List<FinSubjectMonth> list = finSubjectMonthService.selectFinSubjectMonthList(finSubjectMonth);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('project:FinSubject:add')")
    @Log(title = "导入默认数据", businessType = BusinessType.INSERT)
    @PostMapping("/importDefaultFinSubjectMonth")
    public void importDefaultFinSubjectMonth() throws IOException {
        finSubjectMonthService.importDefaultFinSubjectMonth(ServletUtils.getPackageId(),2024,10);
    }

    /**
     * 导出科目余额列表
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubjectMonth:export')")
    @Log(title = "科目余额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinSubjectMonth finSubjectMonth)
    {
        List<FinSubjectMonth> list = finSubjectMonthService.selectFinSubjectMonthList(finSubjectMonth);
        ExcelUtil<FinSubjectMonth> util = new ExcelUtil<FinSubjectMonth>(FinSubjectMonth.class);
        util.exportExcel(response, list, "科目余额数据");
    }

    /**
     * 获取科目余额详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubjectMonth:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(finSubjectMonthService.selectFinSubjectMonthById(id));
    }

    /**
     * 新增科目余额
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubjectMonth:add')")
    @Log(title = "科目余额", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinSubjectMonth finSubjectMonth)
    {
        return toAjax(finSubjectMonthService.importDefaultFinSubjectMonth(finSubjectMonth));
    }

    /**
     * 修改科目余额
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubjectMonth:edit')")
    @Log(title = "科目余额", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinSubjectMonth finSubjectMonth)
    {
        return toAjax(finSubjectMonthService.updateFinSubjectMonth(finSubjectMonth));
    }

    /**
     * 删除科目余额
     */
    @PreAuthorize("@ss.hasPermi('project:FinSubjectMonth:remove')")
    @Log(title = "科目余额", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(finSubjectMonthService.deleteFinSubjectMonthByIds(ids));
    }
}
